//
//  ScheduleViewController.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/6/6.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "ScheduleViewController.h"
#import <Masonry.h>
#import "Common.h"
#import <MJRefresh.h>
#import "NewBtn.h"
#import "TaskCell.h"
#import "TakeCarDetailVC.h"
#import "DeliverCarDetailVC.h"
#import "RootViewController.h"
@interface ScheduleViewController ()
{
    UIView * nav;
    Common * com;
    UITableView * table;
    NSMutableArray * dataSouceArr;
    NSMutableDictionary * dataSouceDic;
    RootViewController * TabBar;

    int page ;
    int totalPage;
    
    int dataType;
    
}

@end

@implementation ScheduleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"任务";
    
    com = [[Common alloc]init];
    nav = [self createNavView];
    [self createNavTitle:@""];
    page = 1;
    totalPage = 1;
    
    dataSouceArr = [[NSMutableArray alloc]init];
    dataSouceDic = [[NSMutableDictionary alloc]init];
    TabBar = [RootViewController defaultsTabBar];

    [self.view addSubview:nav];
    
    [self createTable];
 
    [self loadData:task_Url];
}


-(void)viewWillAppear:(BOOL)animated{
    
    [TabBar setTabBarHidden:NO];
}


-(void)createTable{
    
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Main_Width,Main_height-64*kHeight) style:UITableViewStylePlain];
    [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    table.delegate = self;
    table.dataSource = self;
    [table.mj_header beginRefreshing];
    table.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    [self.view addSubview:table];
}

-(void)downRefresh{
    
    [table.mj_header endRefreshing];
    [table.mj_footer endRefreshing];
    page = 1;
    
    //    [self judgeNetWork];
    NSLog(@"刷新");
}
-(void)upRefresh{
    
    if (page!=totalPage) {
        page ++;
        //        [self judgeNetWork];
        [table.mj_footer endRefreshing];
    }else{
        table.mj_footer.state = MJRefreshStateNoMoreData;
    }
    [table.mj_header endRefreshing];
    NSLog(@"加载%d%d",page,totalPage);
}

-(void)loadData:(NSString *)url{
    
    dataType = 1;
    int pageSize = 10;
    //    hud = [WKProgressHUD showInView:self.view withText:@"加载中" animated:YES];
    NSString * string = [NSString stringWithFormat:@"%@?pageNo=%d&pageSize=%d",url,page,pageSize];
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj){
        
        dataSouceDic = responseObj[@"data"];
        dataSouceArr = dataSouceDic[@"orders"];
        totalPage = [dataSouceDic[@"page"][@"totalPage"] intValue];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //            [hud dismiss:YES];
            [table reloadData];
        });
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //        [hud dismiss:YES];
    });
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *str=@"str";
    
    TaskCell * cell =  [tableView dequeueReusableCellWithIdentifier:str];
    if (cell ==nil) {
        cell = [[TaskCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSLog(@"%ld",(long)indexPath.row);
    
    [cell showInfo:dataSouceArr[indexPath.row]];
    
    cell.Btn3.indexpath = indexPath;
    [cell.Btn3 addTarget:self action:@selector(pressBtn3:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}


-(void)pressBtn3:(NewBtn*)sender{
    
    
//    int statu = [dataSouceArr[sender.indexpath.row][@"status"] intValue];
//
//    switch (statu) {
//        case 40: //前往提车 departure_Url
//        {
//            NSMutableDictionary * dic =[[NSMutableDictionary alloc]init];
//            [dic setObject:dataSouceArr[sender.indexpath.row][@"id"] forKey:@"id"];
//            [self departure:dic];
//            
//        }
//            break;
//        case 45://现场提车
//        {
//            TakeCarDetailVC * detail = [[TakeCarDetailVC alloc]init];
//            detail.orderID = dataSouceArr[sender.indexpath.row][@"id"];
//            [self.navigationController pushViewController:detail animated:YES];
//            
//        }
//            break;
//        case 50://送达交车
//        {
//            DeliverCarDetailVC * detail = [[DeliverCarDetailVC alloc]init];
//            detail.orderID = dataSouceArr[sender.indexpath.row][@"id"];
//            [self.navigationController pushViewController:detail animated:YES];
//        }
//            break;
//        default:
//            break;
//    }
    
    NSLog(@"Btn3:%ld",(long)sender.indexpath.row);
    
}

//前往提车
-(void)departure:(NSMutableDictionary *)dic{

    
    [Common afPostRequestWithUrlString:departure_Url parms:dic finishedBlock:^(id responseObj) {
        NSMutableDictionary * dicObj =[NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dicObj);
        NSString * success = [NSString stringWithFormat:@"%@",dicObj[@"success"]];
        if ([success isEqualToString:@"1"]) {

            [self loadData:task_Url];
            
        }else{
            [self createUIAlertController:[NSString stringWithFormat:@"%@",dicObj[@"message"]]];
        }
        
    } failedBlock:^(NSString *errorMsg) {
        
    }];



}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 240*kHeight;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dataSouceArr.count;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)createUIAlertController:(NSString*)title

{
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action =[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    UIAlertAction * action1 =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action1];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}


@end
